package excel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import string.UtilString;

import java.util.ArrayList;
import java.util.List;

/**
 * Attention:rowIndex,columnIndex both begin with 0.
 *
 * Description: find cells according conditions.
 * Created by 4790K_NSL on 2017/4/29.
 */
public class UtilExcelLocateCell {

    /**在sheet内，查找 cell字符串形式和keyString 一致的cell, 将招的excelPoint放入数组，并返回.
     * @param sheet
     * @param keyString
     * @return
     */
    static  public List<ExcelPoint> locateCellByString(Sheet sheet, String keyString){
        List<ExcelPoint> list=new ArrayList<>();
        if(sheet==null || keyString ==null){
            return list;
        }
        for (Row row : sheet ) {
            list.addAll(locateCellInRowByString(row,keyString));
        }
        return list;
    }
    static  public List<ExcelPoint>  locateCellInColumnByString(Workbook workbook,String sheetName,int columnIndex,String keyString){
        return locateCellInColumnByString(workbook.getSheet(sheetName),columnIndex,keyString);
    }

    static  public List<ExcelPoint>  locateCellInColumnByString(Workbook workbook,String sheetName,String columnAddress,String keyString){
        int columnIndex=UtilExcelCoordinateCellAddress.convertColumnString2ColumnIndex(columnAddress);
        return locateCellInColumnByString(workbook.getSheet(sheetName),columnIndex,keyString);
    }

    /**在sheet指定的column列内，查找 cell字符串形式和keyString 一致的cell, 将招的excelPoint放入数组，并返回.
     * @param sheet
     * @param columnIndex
     * @param keyString
     * @return
     */
    static  public List<ExcelPoint>  locateCellInColumnByString(Sheet sheet, int columnIndex,String keyString){
        List<ExcelPoint> list=new ArrayList<>();
        if(sheet==null || keyString ==null || columnIndex<0){
            return list;
        }
        for (Row row : sheet ) {
            Cell cell=row.getCell(columnIndex);
            String cellValue= UtilExcelCellValue.getCellValueAsString(cell);
            if (cellValue != null) {
                if(UtilString.compareIgnoreCaseAndSpace(cellValue,keyString)){
                    ExcelPoint point=new ExcelPoint(cell.getRowIndex(),cell.getColumnIndex());
                    list.add(point);
                }
            }
        }
        return list;
    }

    static  public List<ExcelPoint>  locateCellInRowByString(Workbook workbook,int rowIndex,String sheetName,String keyString){
        return locateCellInRowByString(workbook.getSheet(sheetName),rowIndex,keyString);
    }

    /**在sheet指定的rowIndex行内，查找 cell字符串形式和keyString 一致的cell, 将招的excelPoint放入数组，并返回.
     * @param sheet
     * @param rowIndex
     * @param keyString
     * @return
     */
    static  public List<ExcelPoint>  locateCellInRowByString(Sheet sheet, int rowIndex,String keyString){
        List<ExcelPoint> list=new ArrayList<>();
        if(sheet==null || keyString ==null || rowIndex<0){
            return list;
        }
        Row row=sheet.getRow(rowIndex);
        List rowList=locateCellInRowByString(row,keyString);
        list.addAll(rowList);
        return list;
    }


    /**在sheet指定的rowIndex行内，查找 cell字符串形式和keyString 一致的cell, 将招的excelPoint放入数组，并返回.
     * @param row
     * @param keyString
     * @return
     */
    static  private List<ExcelPoint> locateCellInRowByString(Row row,String keyString){
        List<ExcelPoint> list=new ArrayList<>();
        if(row==null || keyString==null){
            return  list;
        }else{
            for(Cell cell:row){
                String cellValue= UtilExcelCellValue.getCellValueAsString(cell);
                if(!UtilString.isNullOrEmpty(cellValue,keyString)){
                    if(UtilString.compareIgnoreCaseAndSpace(cellValue,keyString)){
                        ExcelPoint point=new ExcelPoint(cell.getRowIndex(),cell.getColumnIndex());
                        list.add(point);
                    }
                }
            }
        }
        return list;
    }



}
